/**
 * Description:
 * Class Name:Orientation
 * Date:2022/5/3 14:09
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第15章_输入输出._15_8_Java9改进的对象序列化._15_8_5_自定义序列化;

import java.io.*;

public class Orientation implements java.io.Serializable{
    private static final Orientation HORIZONTAL = new Orientation(1);
    private static final Orientation VERTICAL = new Orientation(2);
    private int value;
    private Orientation(int value){
        this.value = value;
    }
    private Object readResolve() throws ObjectStreamException{
        if (value == 1) return HORIZONTAL;
        if (value == 2) return VERTICAL;
        return null;
    }

    public static void main(String[] args) {
        try (
                var oos = new ObjectOutputStream(new FileOutputStream("Chapter_15_8_5_Orientation.txt"));
                var ois = new ObjectInputStream(new FileInputStream("Chapter_15_8_5_Orientation.txt"))
                ){
            oos.writeObject(Orientation.HORIZONTAL);
            var ori = (Orientation) ois.readObject();
            System.out.println(ori);
            System.out.println(Orientation.HORIZONTAL);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}
